package com.codegeek.project.slope.service.impl;

import com.codegeek.common.exception.CustomException;
import com.codegeek.common.utils.DateUtils;
import com.codegeek.common.utils.StringUtils;
import com.codegeek.project.slope.domain.SlopeSupplier;
import com.codegeek.project.slope.mapper.SlopeSupplierMapper;
import com.codegeek.project.slope.service.ISlopeSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 供应厂商Service业务层处理
 *
 * @author codegeek
 * @date 2020-02-17
 */
@Service
public class SlopeSupplierServiceImpl implements ISlopeSupplierService {
    @Autowired
    private SlopeSupplierMapper slopeSupplierMapper;

    /**
     * 查询供应厂商
     *
     * @param supplierId 供应厂商ID
     * @return 供应厂商
     */
    @Override
    public SlopeSupplier selectSlopeSupplierById(Long supplierId) {
        return slopeSupplierMapper.selectSlopeSupplierById(supplierId);
    }

    /**
     * 查询供应厂商列表
     *
     * @param slopeSupplier 供应厂商
     * @return 供应厂商
     */
    @Override
    public List<SlopeSupplier> selectSlopeSupplierList(SlopeSupplier slopeSupplier) {
        return slopeSupplierMapper.selectSlopeSupplierList(slopeSupplier);
    }

    /**
     * 新增供应厂商
     *
     * @param slopeSupplier 供应厂商
     * @return 结果
     */
    @Override
    public int insertSlopeSupplier(SlopeSupplier slopeSupplier) {
        slopeSupplier.setCreateTime(DateUtils.getNowDate());
        return slopeSupplierMapper.insertSlopeSupplier(slopeSupplier);
    }

    /**
     * 修改供应厂商
     *
     * @param slopeSupplier 供应厂商
     * @return 结果
     */
    @Override
    public int updateSlopeSupplier(SlopeSupplier slopeSupplier) {
        slopeSupplier.setUpdateTime(DateUtils.getNowDate());
        return slopeSupplierMapper.updateSlopeSupplier(slopeSupplier);
    }

    /**
     * 批量删除供应厂商
     *
     * @param supplierIds 需要删除的供应厂商ID
     * @return 结果
     */
    @Override
    public int deleteSlopeSupplierByIds(Long[] supplierIds) {
        return slopeSupplierMapper.deleteSlopeSupplierByIds(supplierIds);
    }

    /**
     * 删除供应厂商信息
     *
     * @param supplierId 供应厂商ID
     * @return 结果
     */
    @Override
    public int deleteSlopeSupplierById(Long supplierId) {
        return slopeSupplierMapper.deleteSlopeSupplierById(supplierId);
    }

    /**
     * 导入
     *
     * @param slopeSupplier
     * @param operName
     * @return
     */
    @Override
    public String importDevice(List<SlopeSupplier> slopeSupplier, boolean updateSupport, String operName) {
        if (StringUtils.isNull(slopeSupplier) || slopeSupplier.size() == 0) {
            throw new CustomException("导入设备数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SlopeSupplier device : slopeSupplier) {
            try {
                // 验证是否存在这个设备
                SlopeSupplier checkDevice = new SlopeSupplier();
                checkDevice.setSupplierName(device.getSupplierName());
                device.setSupplierRoughlyAddress(device.getSupplierRoughlyAddress().split(" ").length == 3 ? device.getSupplierRoughlyAddress() : "北京市 市辖区 东城区");
                List<SlopeSupplier> flag = selectSlopeSupplierList(checkDevice);
                if (flag.size() == 0) {
                    this.insertSlopeSupplier(device);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、供应商 " + device.getSupplierName() + " 导入成功");
                } else if (updateSupport) {
                    device.setUpdateBy(operName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、供应商 " + device.getSupplierName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、供应商 " + device.getSupplierName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备 " + device.getSupplierName() + " 导入失败：";
                failureMsg.append(msg);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 禁用供应厂商信息
     *
     * @param supplierId 供应厂商ID
     * @return 结果
     */
    @Override
    public int disableSlopeSupplierByIds(Long[] supplierId) {
        return slopeSupplierMapper.disableSlopeSupplierByIds(supplierId);
    }

    /**
     * 启用供应厂商信息
     *
     * @param supplierId 供应厂商ID
     * @return 结果
     */
    @Override
    public int enableSlopeSupplierByIds(Long[] supplierId) {
        return slopeSupplierMapper.enableSlopeSupplierByIds(supplierId);
    }


    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 检查供应商名字是否重复
     *
     * @param supplierName
     * @return
     */
    @Override
    public List<Map<String, Object>> checkSupplierName(String supplierName) {
        StringBuilder sql = new StringBuilder("SELECT * FROM slope_supplier WHERE supplier_name = '" + supplierName + "'");
        return jdbcTemplate.queryForList(sql.toString());
    }

}
